package com.ytkj.ygAssist.main;

import com.ytkj.ygAssist.server.GetGoodsInfo;
import com.ytkj.ygAssist.server.GetUserBuyServer;
import com.ytkj.ygAssist.server.SelectAssistPublishs;
import com.ytkj.ygAssist.server.util.HttpGetUtil;
import com.ytkj.ygAssist.tools.CacheData;
import com.ytkj.ygAssist.tools.JFrameListeningInterface;
import com.ytkj.ygAssist.tools.MyLog;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/*
 * 走势分析服务
 */
public class TrendChartServer {
	public static void getTrendChart(final String goodsID, String EIdx, JFrameListeningInterface foreknowInterface) {
		if (CacheData.getGoodsNameCacheDate(goodsID) == null) {
			if (GetGoodsInfo.getGoodsInfoByGoodsID(goodsID)) {
				foreknowInterface.setFrameText("setGoodsName", CacheData.getGoodsNameCacheDate(goodsID));
			} else {
				foreknowInterface.setFrameText("setGoodsNameError", null);
				return;
			}
		} else {
			CacheData.setGoodsTreeListCacheDate(goodsID);
			foreknowInterface.setFrameText("setGoodsName", CacheData.getGoodsNameCacheDate(goodsID));
		}
//		String text[] = GetGoodsInfo.shopCartNew(CacheData.getGoodsInfoCacheDate(goodsID)[2],
//				HttpGetUtil.getHttpClient());
//		String text[] = GetGoodsInfo.shopCartNewByHtml(goodsID,
//				HttpGetUtil.getHttpClient());
		String text[] = GetGoodsInfo.getGoodsSalingByGoodsID(goodsID,
				HttpGetUtil.getHttpClient());
		foreknowInterface.setFrameListeningText("1", text);
		boolean isHavaData = true;
		ConcurrentHashMap<String, String[]> selectCacheDateMap = CacheData.getSelectCacheDate(goodsID);
		try {
			int newestPeriod = Integer.parseInt(text[1]);
			int EIdxInt = Integer.parseInt(EIdx);
			for (int i = 1; i < EIdxInt && i < newestPeriod; i++) {
				String[] cacheDateStr = selectCacheDateMap.get(Integer.toString(newestPeriod - i));
				if (cacheDateStr == null) {
					isHavaData = false;
				} else {
					foreknowInterface.setFrameListeningText("3", cacheDateStr);
				}
			}
			CacheData.setGoodsPriceCacheDate(goodsID, Integer.parseInt(text[3]));
		} catch (Exception e) {
		}
		if (!isHavaData) {
			SelectAssistPublishs.getYungouPublishs(goodsID, "0", EIdx);
		}
		List<Map<String, String>> contentList = GetGoodsInfo.getBarcodeRaffListByGoodsID(goodsID, EIdx);
		if (contentList != null) {
			for (final Map<String, String> map : contentList) {
				final String codePeriod = map.get("codePeriod");
				String[] SelectText = selectCacheDateMap.get(codePeriod);
				final String codeRNO = map.get("codeRNO");
				if (SelectText != null) {
					if (!codeRNO.equals("") && !SelectText[2].equals(codeRNO)) {
						MyLog.outLog("计算结果错误了", goodsID, codePeriod, "原", SelectText[2], "正确", codeRNO);
						SelectAssistPublishs.deleteYungouPublish(goodsID, codePeriod);
						CacheData.removeSelectCacheDate(goodsID, codePeriod);
						SelectText = null;
					}
				}
				if (SelectText == null) {
					Thread thread = new Thread(new Runnable() {
						public void run() {
							if (map.get("codeState").equals("1")) {
							} else {
								String codeID = map.get("codeID");
								if (codeRNO.equals("")) {
									GetUserBuyServer.getUserBuyServer(codeID).GetGoodsPeriodInfo(goodsID, codePeriod,
											codeID, false);
								} else {
									GetUserBuyServer getUserBuyServer = GetUserBuyServer.getUserBuyServer(codeID);
									getUserBuyServer.setBarcodernoInfo(codeRNO, map.get("userName"),
											map.get("userWeb"));
									getUserBuyServer.GetGoodsPeriodInfo(goodsID, codePeriod, codeID, false);
								}
							}
						}
					});
					thread.start();
				} else {
					foreknowInterface.setFrameListeningText("3", SelectText);
				}
			}
		} else {
			foreknowInterface.setFrameText("setGoodsNameError", null);
		}
	}
}
